package com.timedomain.him.remake.core;
import java.util.Timer;

import static com.un4seen.bass.BASS.*;


public class SimplePlayer extends BasePlayer {

    private int channelHandle = 0;

    public SimplePlayer(int channelHandle){
        this.channelHandle = channelHandle;

    }

    @Override
    public void start(){
        BASS_ChannelPlay(channelHandle,false);
    }

    @Override
    public float getPeak() {
        return BassUtils.GetPeak(channelHandle);
    }


    public void restart(){
        BASS_ChannelPlay(channelHandle,true);
    }

    @Override
    public void pause(){
        BASS_ChannelPause(channelHandle);
    }

    @Override
    public void stop() {
        BASS_ChannelPause(channelHandle);
        BASS_ChannelSetPosition(channelHandle,0,BASS_POS_BYTE);
    }

    public void setLooping(boolean isLooping){
        if(isLooping){

            BASS_ChannelFlags(channelHandle,BASS_SAMPLE_LOOP,BASS_SAMPLE_LOOP);
        }
        else{
            BASS_ChannelFlags(channelHandle,BASS_SAMPLE_LOOP,~BASS_SAMPLE_LOOP);
        }
    }


    @Override
    public void close() {
        if(isPlaying()){
            stop();
        }
        BASS_ChannelFree(channelHandle);
    }

    @Override
    public long getDuration() {
        return BassUtils.GetDuration(channelHandle);
    }

    @Override
    public long getPosition() {
        return BassUtils.GetPosition(channelHandle);
    }

    @Override
    public boolean isStopped() {
        return !isPlaying() && getDuration() - getPosition() < 500;
    }

    private float volume = 1;

    @Override
    public void setVolume(float volume) {
        this.volume = volume;
        BASS_ChannelSetAttribute(channelHandle,BASS_ATTRIB_VOL,volume);
    }

    @Override
    public float getVolume() {
        return volume;
    }

    @Override
    public void setPosition(long pos) {
        BassUtils.SetPosition(channelHandle,pos);
    }

    @Override
    public boolean isPlaying() {
        return BASS_ChannelIsActive(channelHandle) == BASS_ACTIVE_PLAYING;
    }
}
